package md.mru.frontend.controllers;

import md.mru.core.domains.type.EventType;
import md.mru.core.domains.type.OrderStateType;
import md.mru.core.dtos.*;
import md.mru.core.service.EventService;
import md.mru.core.service.OrderService;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 31/05/2014.
 */
@ManagedBean
@SessionScoped
public class UtilAdministrationBean implements Serializable {

    private static final long serialVersionUID = 224429511258316439L;

    private String breadCrump;

    private static final int EVENTS_SIZE = 20;

    @ManagedProperty(value = "#{eventService}")
    private EventService eventService;
    @ManagedProperty(value = "#{authenticationBean}")
    private AuthenticationBean authenticationBean;
    /*@ManagedProperty(value = "#{orderService}")
    private OrderService orderService;*/
    private List<EventDto> events = new ArrayList<>();
    /*private List<OrderTypeGroupDto> orderTypeGroups;
    private List<OrderTypeDto> orderTypes;
    private OrderTypeDto selectedOrderType;*/
    private long elementsNo;

    /*public void initOrderTemplates() {
        if (this.orderTypeGroups == null) {
            this.orderTypeGroups = orderService.loadOrderTypeGroups();
            //getObjectMappingConverter().mapObjectList(orderTypeGroups);
        }
    }

    public void onOrderGroupSelect(AjaxBehaviorEvent event) {
        Long value = (Long) ((UIOutput) event.getSource()).getValue();
        //OrderStateType orderStateType = OrderStateType.valueOf(value.getId());
        this.orderTypes = orderService.loadOrderTypesByGroup(value);
        getObjectMappingConverter().mapObjectList(orderTypes);
    }*/

    /*public void onOrderTypeSelect(AjaxBehaviorEvent event) {
        Long value = (Long) ((UIOutput) event.getSource()).getValue();
        OrderStateType orderStateType = OrderStateType.valueOf(value.getId());
        this.orderTypes = orderService.loadOrderTypesByGroup(value.getId());
        getObjectMappingConverter().mapObjectList(orderTypes);
    }*/

    public void loadNextEvents() {
        //TODO - uncomment when will be reched this place for dev
        /*long userId = authenticationBean.getLoginUser().getUser().getId();
        long groupId = authenticationBean.getLoginUser().getUser().getUserGroup().getId();
        EventsWrapper eventsWrapper = eventService.loadNextEvents(userId, groupId, events.size(), EVENTS_SIZE);
        this.events.addAll(eventsWrapper.getEvents());
        elementsNo = eventsWrapper.getTotalElements() - this.events.size();*/
    }

    public void createNewEvent(EventDto eventDto) {
        // TODO - add logged user group
        //eventDto.setCreatedByUser(new UserDto());
        eventService.createNewEvent(eventDto);
    }

    public boolean orderEventType(EventType type) {
        return type != null && type.equals(EventType.ORDER);
    }

    public boolean treeEventType(EventType type) {
        return type != null && type.equals(EventType.TREE);
    }

    public void setEventService(EventService eventService) {
        this.eventService = eventService;
    }

    public List<EventDto> getEvents() {
        return events;
    }

    public String getBreadCrump() {
        return breadCrump;
    }

    public void setBreadCrump(String breadCrump) {
        this.breadCrump = breadCrump;
    }

    public void setAuthenticationBean(AuthenticationBean authenticationBean) {
        this.authenticationBean = authenticationBean;
    }

    /*public List<OrderTypeGroupDto> getOrderTypeGroups() {
        return orderTypeGroups;
    }

    public List<OrderTypeDto> getOrderTypes() {
        return orderTypes;
    }

    public OrderTypeDto getSelectedOrderType() {
        return selectedOrderType;
    }

    public void setSelectedOrderType(OrderTypeDto selectedOrderType) {
        this.selectedOrderType = selectedOrderType;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }*/
}
